/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;


public class Rocket extends GameObject implements Steppable{
    private Part part;
    private float force;
    private boolean running = false;
    public Rocket() throws WorldsException
    {
        super("Rocket");
        addPropChangeHandler("Parent", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject) oldValue, (GameObject) value);
                if(value instanceof Part)
                {
                    if(oldValue instanceof Part)
                        stop();
                    start((Part)value);
                }
            }
        });
        addPropChangeHandler("Force", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                force = (Float) value;
            }
        });
        //x_;
    }
    @Override
    public void step()
    {
        if(force != 0 && part != null && running && !part.getBody().isStatic())
        {
            Vec2D look = new Vec2D(part.getBody().getAngle() + part.rotationDiff + Math.PI / 2);
            Vec2D rocket = look.mul(force).mul(part.getBody().getMass()/2);
            part.getBody().applyForce(rocket, part.getPosition());
            part.getBody().wakeUp();
            part.wakeUpConnected();
        }
    }
    private void start(Part p)
    {
        part = p;
        running = true;
    }
    private void stop()
    {
        running = false;
    }
    @Override
    public void destructor()
    {
        stop();
    }
}
